import bean.Ticket;

public class Main {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        //创建生产彩票和购买彩票的任务
        Runnable produce = new produce(ticket);
        Runnable get = new get(ticket);
        //创建一个线程分配生产彩票的任务
        Thread thread = new Thread(produce);
        //创建四个线程都分配同一个购买彩票的任务
        Thread t1 = new Thread(get);
        Thread t2 = new Thread(get);
        Thread t3 = new Thread(get);
        Thread t4 = new Thread(get);
        thread.start();
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
    //生产者任务
    static class produce implements Runnable {
        private Ticket ticket;
        public produce(Ticket ticket) {
            this.ticket = ticket;
        }
        //生产彩票任务
        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket.produceTicket();
            }

        }
    }
    //消费者任务
    static class get implements Runnable {
        private Ticket ticket;
        public get(Ticket ticket) {
            this.ticket = ticket;
        }
        //消费彩票的任务
        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket.getTicket();
            }
        }
    }
}